﻿package map {

	import fl.controls.Label;
	import flash.net.*;
	import flash.events.*;
	import flash.display.*;
	import flash.text.*;
	import fl.transitions.Tween;
	import fl.transitions.easing.Strong;

	public class Map extends MovieClip {

		private var mapLayer:MovieClip = new MovieClip();
		private var lineLayer:MovieClip = new MovieClip();
		private var positionLayer:MovieClip = new MovieClip();
		private var menuLayer:MovieClip = new MovieClip();

		private var strXMLPath:String = "mapXML.xml";
		private var xmlMap:XML;

		var headlineTextFormat:TextFormat;
		var listTextFormat:TextFormat;
		var uniNameTextFormat:TextFormat;

		public function Map() {
			this.addChildAt(this.mapLayer, 0);
			this.addChildAt(this.lineLayer, 1);
			this.addChildAt(this.positionLayer, 2);
			this.addChildAt(this.menuLayer, 3);
			headlineTextFormat = new TextFormat();
			headlineTextFormat.size = 18;
			headlineTextFormat.font = "Arial";
			listTextFormat = new TextFormat();
			listTextFormat.size = 14;
			listTextFormat.font = "Arial";
			uniNameTextFormat = new TextFormat();
			uniNameTextFormat.size = 16;
			uniNameTextFormat.font = "Arial";
			this.loadXML();
		}

		private function loadXML():void {
			var xmlLoader:URLLoader = new URLLoader();
			xmlLoader.load(new URLRequest(strXMLPath));
			xmlLoader.addEventListener(Event.COMPLETE, function(event:Event) {
				xmlMap = new XML(event.target.data);
				setPoints();
			});
		}

		private function setPoints():void {

			// Karte anzeigen
			var myMap:Bitmap = new Bitmap();
			myMap.bitmapData = new GermanyMap(0,0);
			myMap.visible = true;
			this.mapLayer.addChild(myMap);

			// Positionen in Form von Kreisen auf Karte eintragen
			var i:int = 0;
			for each (var num:XML in this.xmlMap.position) {
				var c:Position = new Position();
				c.x = num.@left;
				c.y = num.@top;
				c.position = i;

				// Unis pro Position zählen
				var universityCounter:int = 0;
				for each (var uni:XML in num.city) {
					universityCounter = universityCounter + uni.university.length();
				}

				// Je mehr Unis, desto größer der Kreis
				if ((20 + (2 * universityCounter)) > 30) {
					c.height = 30;
					c.width = 30;
				} else {
					c.height = 20 + (2 * universityCounter);
					c.width = 20 + (2 * universityCounter);
				}

				c.addEventListener(MouseEvent.CLICK, function(event:MouseEvent) {
					showUniversities(0, Position(event.currentTarget));
				});

				positionLayer.addChildAt(c, i);

				i++;
			}
		}

		private function showUniversities(page:int, position:Position):void {

			this.clearLayer();
			this.endAnimations();

			var elementsPerPage:int = 14;
			var pageElements:int = page * elementsPerPage;

			// Animation für den ausgewählten Kreis starten
			position.gotoAndPlay(2);

			// Popup anzeigen
			var popup:Popup = this.createPopup();

			// Unis hinzufügen
			var universityCounter:int = 0;
			var i:int = 0;
			for each (var city:XML in this.xmlMap.position[position.position].city) {
				for each (var uni:XML in city.university) {
					if ((universityCounter >= pageElements) && (universityCounter < (pageElements + elementsPerPage))) {
						var u:University = new University();
						u.x = -290;
						u.y = (i * 24) - 170;
						u.width = 550;
						u.height = 35;
						u.text = uni.name + " (" + city.name + ")";
						if (u.text.length > 75) {
							u.text = u.text.substr(0, 75) + "...";
						}
						u.setStyle("textFormat", this.listTextFormat);
						u.universityId = uni.@id;
						u.universityName = uni.name;
						u.page = page;
						u.position = position;
						u.addEventListener(MouseEvent.CLICK, function(event:MouseEvent) {
							showCourses(0, University(event.currentTarget));
						});
						popup.addChild(u);
						i++;
					}
					universityCounter++;
				}
			}

			// Zurück-Button
			if (page > 0) {
				var backButton:RoundButton = new RoundButton();
				backButton.gotoAndStop(3);
				backButton.x = -280;
				backButton.y = 190;
				backButton.height = 20;
				backButton.width = 20;
				backButton.addEventListener(MouseEvent.CLICK, function() {
					showUniversities(page - 1, position);
				});
				popup.addChild(backButton);
			}

			// Weiter-Button
			if (universityCounter > (pageElements + elementsPerPage)) {
				var forwardButton:RoundButton = new RoundButton();
				forwardButton.gotoAndStop(2);
				forwardButton.x = 280;
				forwardButton.y = 190;
				forwardButton.height = 20;
				forwardButton.width = 20;
				forwardButton.addEventListener(MouseEvent.CLICK, function() {
					showUniversities(page + 1, position);
				});
				popup.addChild(forwardButton);
			}

			this.menuLayer.addChildAt(popup, 0);
		}

		private function showCourses(page:int, university:University):void {
			
			var elementsPerPage:int = 14;
			var pageElements:int = page * elementsPerPage;
			var l:Course;

			this.clearLayer();

			// Popup anzeigen
			var popup:Popup = this.createPopup();

			// Headline
			var headline = new Label();

			headline.text = university.universityName;
			if (headline.text.length > 55) {
				headline.text = headline.text.substr(0, 55) + "...";
			}
			headline.setStyle("textFormat", this.headlineTextFormat);
			headline.x = -260;
			headline.y = -202;
			headline.width = 500;
			headline.height = 35;
			popup.addChild(headline);

			// Hoch-Button
			var upButton:RoundButton = new RoundButton();
			upButton.gotoAndStop(4);
			upButton.x = -278;
			upButton.y = -190;
			upButton.height = 20;
			upButton.width = 20;
			upButton.addEventListener(MouseEvent.CLICK, function() {
				showUniversities(university.page, university.position);
			});
			popup.addChild(upButton);

			// Für jede Stadt
			var courseCounter:int = 0;
			var i:int = 0;
			for each (var city:XML in this.xmlMap.position[university.position.position].city) {
				// Für jede Universität
				for each (var uni:XML in city.university) {
					if (uni.@id == university.universityId) {
						for each (var course:XML in uni.course) {
							if ((courseCounter >= pageElements) && (courseCounter < (pageElements + elementsPerPage))) {
								l = new Course();
								l.courseId = course.@id;
								l.university = university;
								l.text = course.name;
								l.setStyle("textFormat", this.listTextFormat);
								l.x = -290;
								l.y = (i * 24) - 170;
								l.width = 560;
								l.height = 35;
								l.addEventListener(MouseEvent.CLICK, function(event:MouseEvent) {
									connectCourses(Course(event.currentTarget));
								});
								popup.addChild(l);
								i++;
							}
							courseCounter++;
						}
					}
				}
			}

			// Zurück-Button
			if (page > 0) {
				var backButton:RoundButton = new RoundButton();
				backButton.gotoAndStop(3);
				backButton.x = -280;
				backButton.y = 190;
				backButton.height = 20;
				backButton.width = 20;
				backButton.addEventListener(MouseEvent.CLICK, function() {
					showCourses(page - 1, university);
				});
				popup.addChild(backButton);
			}

			// Weiter-Button
			if (courseCounter > (pageElements + elementsPerPage)) {
				var forwardButton:RoundButton = new RoundButton();
				forwardButton.gotoAndStop(2);
				forwardButton.x = 280;
				forwardButton.y = 190;
				forwardButton.height = 20;
				forwardButton.width = 20;
				forwardButton.addEventListener(MouseEvent.CLICK, function() {
					showCourses(page + 1, university);
				});
				popup.addChild(forwardButton);
			}

			this.menuLayer.addChildAt(popup, 0);
		}

		private function connectCourses(course:Course):void {

			this.clearLayer();

			this.lineLayer.graphics.lineStyle(2, 0x65CCFF, .75);

			// Suche nach Studiengängen an anderen Positionen
			var foundAt:int = 0;
			var i:int = 0;
			for each (var pos:XML in this.xmlMap.position) {
				for each (var city:XML in pos.city) {
					for each (var uni:XML in city.university) {
						for each (var courses:XML in uni.course) {
							if (courses.@id == course.courseId) {
								Position(this.positionLayer.getChildAt(foundAt)).gotoAndPlay(20);
								this.lineLayer.graphics.moveTo(course.university.position.x, course.university.position.y);
								this.lineLayer.graphics.lineTo(this.positionLayer.getChildAt(foundAt).x, this.positionLayer.getChildAt(foundAt).y);
								var uniNameLayer:UniNameLayer = new UniNameLayer();
								uniNameLayer.width = 200;
								uniNameLayer.height = 20;
								uniNameLayer.alpha = 0.8;
								uniNameLayer.x = int(pos.@left) + (uniNameLayer.width / 2) + 10;
								uniNameLayer.y = int(pos.@top) + (uniNameLayer.height / 2);
								var uniTextLabel:Label = new Label();
								uniTextLabel.width = 180;
								uniTextLabel.height = 25;
								uniTextLabel.x = -90;
								uniTextLabel.y = -10;
								uniTextLabel.text = uni.name;
								if (uniTextLabel.text.length > 30) {
									uniTextLabel.text = uniTextLabel.text.substr(0, 30) + "...";
								}
								uniTextLabel.setStyle("textFormat", this.uniNameTextFormat);
								uniNameLayer.addChild(uniTextLabel);
								this.menuLayer.addChildAt(uniNameLayer, i);
								i++;
							}
						}
					}
				}
				foundAt++;
			}
		}
		
		private function clearLayer():void {

			// Bisher aufgeklappte Menüs entfernen
			var numChildren:int = this.menuLayer.numChildren - 1;
			while (numChildren >= 0) {
				this.menuLayer.removeChildAt(numChildren);
				numChildren--;
			}

			// Verbindungslinien ausblenden
			this.lineLayer.graphics.clear();

		}

		private function endAnimations():void {

			// Alle laufenden Animationen beenden
			var p:Position;
			for (var i:int = 0; i < this.positionLayer.numChildren; i++) {
				p = Position(this.positionLayer.getChildAt(i));
				p.gotoAndStop(1);
			}

		}

		private function createPopup():Popup {
			
			// Neues Popup erstellen
			var popup:Popup = new Popup();
			popup.width = 600;
			popup.height = 420;
			popup.x = 450;
			popup.y = 400;
			
			new Tween(popup, "alpha", Strong.easeOut, 0, 0.8, 3, true);
			new Tween(popup, "y", Strong.easeOut, 0, 400, 2, true);

			var closeButton:RoundButton = new RoundButton();
			closeButton.width = 20;
			closeButton.height = 20;
			closeButton.x = 280;
			closeButton.y = -190;
			closeButton.gotoAndStop(1);
			closeButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent) {
				new Tween(popup, "alpha", Strong.easeOut, 0.8, 0, 3, true);
				new Tween(popup, "y", Strong.easeOut, 400, -220, 2, true);
				endAnimations();
			});

			popup.addChild(closeButton);

			return popup;

		}

	}

}